interface PermissionConfig {
	// 授权码，如：scope.userLocation
	code: string
	// 是否获取订阅参数
	withSubscriptions?: boolean
	// 确认框标题
	title?: string
	// 确认框内容
	message?: string
	// 确认框确认按钮文本
	confirmText?: string
	// 确认框取消按钮文本
	cancelText?: string
}

/**
 * 获取授权
 * @param {*} { code, title, message, withSubscriptions }
 * @return Promise<boolean>，已授权then，未授权catch
 */
export const permissionGet = ({
	code,
	withSubscriptions = false,
	title,
	message,
	confirmText,
	cancelText
}: PermissionConfig): Promise<boolean> => {
	return new Promise((resolve, reject) => {
		// #ifdef APP
		resolve(true)
		// #endif
		// #ifndef APP
		uni.getSetting({
			withSubscriptions,
			success: async ({
				authSetting = {},
				subscriptionsSetting = {}
			}: AnyObject) => {
				if (withSubscriptions) {
					const subscript = onSubscriptionsStatus(
						subscriptionsSetting,
						code
					)
					if (subscript === 'accept') {
						// 已接受
						resolve(true)
					} else if (subscript === 'reject') {
						// 已拒绝
						try {
							await permissionOpen({
								code,
								withSubscriptions,
								title,
								message,
								confirmText,
								cancelText
							})
							resolve(true)
						} catch (error) {
							reject(false)
						}
					} else {
						resolve(false)
					}
				} else {
					console.log('authSetting', authSetting)
					// 没有授权
					if (!authSetting[code]) {
						// 申请授权
						uni.authorize({
							scope: code,
							success: () => {
								resolve(true)
							},
							fail: async () => {
								try {
									await permissionOpen({
										code,
										withSubscriptions,
										title,
										message,
										confirmText,
										cancelText
									})
									resolve(true)
								} catch (error) {
									reject(false)
								}
							}
						})
					} else {
						resolve(true)
					}
				}
			},
			fail: () => {
				reject(false)
			}
		})
		// #endif
	})
}

// 打开授权设置
export const permissionOpen = ({
	code,
	withSubscriptions,
	title = '是否前往授权？',
	message = '',
	confirmText = '确认',
	cancelText = '取消'
}: PermissionConfig) => {
	return new Promise((resolve, reject) => {
		uni.showModal({
			title,
			content: message,
			confirmText,
			cancelText,
			success: (modal) => {
				if (modal.confirm) {
					uni.openSetting({
						withSubscriptions,
						success: ({ authSetting = {} }: AnyObject) => {
							// 判断是否有订阅
							if (withSubscriptions) {
								// 再次获取配置
								uni.getSetting({
									withSubscriptions,
									success: ({
										subscriptionsSetting = {}
									}: AnyObject) => {
										const subscript = onSubscriptionsStatus(
											subscriptionsSetting,
											code
										)
										if (subscript === 'accept') {
											resolve(true)
										} else {
											reject(false)
										}
									}
								})
							} else {
								if (authSetting[code]) {
									resolve(true)
								} else {
									reject(false)
								}
							}
						},
						fail: () => {
							reject(false)
						}
					})
				} else {
					reject(false)
				}
			}
		})
	})
}

// 订阅授权状态
const onSubscriptionsStatus = (
	subscriptionsSetting: AnyObject,
	code: string
) => {
	if (
		subscriptionsSetting.mainSwitch &&
		subscriptionsSetting.itemSettings &&
		subscriptionsSetting.itemSettings[code] == 'accept'
	) {
		// 已接受
		return 'accept'
	} else if (
		!subscriptionsSetting.mainSwitch ||
		(subscriptionsSetting.itemSettings &&
			subscriptionsSetting.itemSettings[code] == 'reject')
	) {
		// 已拒绝
		return 'reject'
	} else {
		return 'none'
	}
}
